package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.ExInfo;
import com.example.demo.mapper.ExInfoMapper;
import com.example.demo.service.ExInfoService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author admin
* @description 针对表【ex_info】的数据库操作Service实现
* @createDate 2024-02-19 10:51:35
*/
@Service
public class ExInfoServiceImpl extends ServiceImpl<ExInfoMapper, ExInfo>
    implements ExInfoService {

    public List<ExInfo> convertToTree(List<ExInfo> exInfos, int parentId, String indexPrefix) {
        List<ExInfo> result = new ArrayList<>();
        int indexId = 0;
        for (ExInfo exInfo : exInfos) {
            if (exInfo.getParentId() == parentId) {
                indexId++;
                String newIndexPrefix = indexPrefix != null ? indexPrefix + "." + indexId : Integer.toString(indexId);
                List<ExInfo> children = convertToTree(exInfos, exInfo.getId(),newIndexPrefix);
                if (!children.isEmpty()) {
                    exInfo.setIndexId(newIndexPrefix);
                    exInfo.setChildren(children);
                } else {
                    exInfo.setIndexId(newIndexPrefix);
                }
                result.add(exInfo);
            }
        }
        return result;
    }

//    public List<ExInfo> updateIsFinished(List<ExInfo> exInfos) {
//        List<ExInfo> result = new ArrayList<>();
//        for (ExInfo exInfo : exInfos) {
//            if (exInfo.getChildren()!=null){
//                List<ExInfo> children=updateIsFinished(exInfos);
//                for (ExInfo child : children) {
//                    if (child.getIsFinished() != 1) {
//                        allChildrenFinished = false;
//                        break;
//                    }
//                }
//
//                if (allChildrenFinished) {
//                    exInfo.setIsFinished(1);
//                }
//            }
//
//        }
//        return result;
//    }
}




